package com.design.oop.util;

import android.content.Context;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;

import com.design.oop.WheatBeerApplication;

public class DisplayUtil {
	private final static float DEFAULT_SCREEN_WIDTH = 480;
	private static int displayWidth;
	private static int displayHeight;
	private static int density;
	private static float displaySizeRatio;

	public static int getDensity() {
		if (density > 0) {
			return density;
		}
		Display display = ((WindowManager)WheatBeerApplication.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

		DisplayMetrics metrics = new DisplayMetrics();
		display.getMetrics(metrics);

		density = metrics.densityDpi;

		return density;
	}

	@SuppressWarnings("deprecation")
	public static int getDisplayWidth() {
		if (displayWidth > 0) {
			return displayWidth;
		}

		Display display = ((WindowManager)WheatBeerApplication.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		displayWidth = display.getWidth();

		return displayWidth;
	}

	@SuppressWarnings("deprecation")
	public static int getDisplayHeight() {
		if (displayHeight > 0) {
			return displayHeight;
		}

		Display display = ((WindowManager)WheatBeerApplication.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		displayHeight = display.getHeight();

		return displayHeight;
	}

	public static float getDisplaySizeRatio() {
		if (displaySizeRatio > 0) {
			return displaySizeRatio;
		}

		int displayWidth = getDisplayWidth();
		float ratio = displayWidth;

		if (displayWidth <= DEFAULT_SCREEN_WIDTH) {
			ratio = 1;
		} else {
			ratio = (float)displayWidth / DEFAULT_SCREEN_WIDTH;
		}

		displaySizeRatio = ratio;

		return displaySizeRatio;
	}

	public static Typeface getTypeface(String typeface) {
		return Typeface.createFromAsset(WheatBeerApplication.getContext().getAssets(), typeface);
	}

	public static int getPixelFromDP(float dpValue) {
		return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getDisplayMetrics());
	}

	public static int getPixelFromSP(float spValue) {
		return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, getDisplayMetrics());
	}

	public static DisplayMetrics getDisplayMetrics() {
		DisplayMetrics displaymetrics = new DisplayMetrics();
		((WindowManager)WheatBeerApplication.getContext().getSystemService("window")).getDefaultDisplay().getMetrics(displaymetrics);
		return displaymetrics;
	}

	public static final int REVISED_TEXT_POSITION_Y = DisplayUtil.getPixelFromDP(1.33f);
}
